package com.neusoft.elm.view.impl;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.Impl.BusinessDaoImpl;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.view.BusinessView;

import java.util.List;
import java.util.Scanner;

/**
 * 商家方法实现
 * view层
 */
public class BusinessViewImpl implements BusinessView {
    Scanner input = new Scanner(System.in);

    /**
     * 列出所有商家信息
     */
    @Override
    public void listBusinessAll() {
        BusinessDao dao = new BusinessDaoImpl();
        List<Business> list = dao.listBusiness(null,null);
        System.out.println("商家编号\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
        for(Business b : list){
            System.out.println(b.getBusinessId()+"\t"
                              +b.getBusinessName()+"\t"
                              +b.getBusinessAddress()+"\t"
                              +b.getBusinessExplain()+"\t"
                              +b.getStarPrice()+"\t"
                              +b.getDeliveryPrice());
        }
    }


    /**
     * 模糊查询商家信息
     */
    @Override
    public void listBusiness() {
        String businessName = "";
        String businessAddress = "";

        String inputStr = "";
        System.out.println("是否输入商家名称关键词(y/n)");
        inputStr = input.next();

        if(inputStr.equals("Y") || inputStr.equals("y")){
            System.out.println("请输入商家名称关键词");
            businessName = input.next();
        }

        System.out.println("是否输入商家地址关键词(y/n)");
        inputStr = input.next();

        if(inputStr.equals("Y") || inputStr.equals("y")){
            System.out.println("请输入商家地址关键词");
            businessAddress = input.next();
        }

        BusinessDao dao = new BusinessDaoImpl();
        List<Business> list = dao.listBusiness(businessName,businessAddress);
        System.out.println("商家编号\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
        for(Business b : list){
            System.out.println(b.getBusinessId()+"\t"
                    +b.getBusinessName()+"\t"
                    +b.getBusinessAddress()+"\t"
                    +b.getBusinessExplain()+"\t"
                    +b.getStarPrice()+"\t"
                    +b.getDeliveryPrice());
        }
    }

    /**
     * 新建商家
     */
    @Override
    public void saveBusiness() {
        System.out.println("请输入商家名称：");
        String businessName = input.next();
        BusinessDao dao = new BusinessDaoImpl();
        int businessId = dao.saveBusiness(businessName);
        if(businessId>0){
            System.out.println("新建商家成功 商家编号为："+businessId);
        }else {
            System.out.println("新建商家失败");
        }
    }

    /**
     * 删除商家信息
     */
    @Override
    public void removeBusiness() {
        System.out.println("请输入商家编号：");
        int businessId = input.nextInt();

        BusinessDao dao = new BusinessDaoImpl();
        System.out.println("确认删除吗(y/n)");
        if(input.next().equals("y") || input.next().equals("Y")){
            int result = dao.removeBusiness(businessId);
            if(result==1){
                System.out.println("删除商家成功");
            }else{
                System.out.println("删除商家失败");
            }
        }
    }

    /**
     * 商家登录
     *
     * @return
     */
    @Override
    public Business login() {
        System.out.println("请输入商家编号");
        int businessId = input.nextInt();
        System.out.println("请输入密码");
        String password = input.next();

        // 调用dao层方法创建Business实体对象
        BusinessDao dao = new BusinessDaoImpl();
        return dao.getBusinessByIdByPass(businessId, password);
    }

    /**
     * 根据编号查询商家
     * @param businessId
     */
    @Override
    public void showBusiness(Integer businessId) {
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        System.out.println(business);
    }

    /**
     * 修改商家信息
     * @param businessId
     */
    @Override
    public void editBusiness(Integer businessId) {
        //显示目前商家信息
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        System.out.println(business);

        String inputStr = "";

        System.out.println("是否修改商家名称(y/n)");
        inputStr = input.next();

        //忽略大小写
        if(inputStr.equals("y") || inputStr.equals("Y")){
            System.out.println("输入新的商家名称");
            business.setBusinessName(input.next());
        }

        System.out.println("是否修改商家地址(y/n)");
        inputStr = input.next();

        //忽略大小写
        if(inputStr.equals("y") || inputStr.equals("Y")){
            System.out.println("输入新的商家地址");
            business.setBusinessAddress(input.next());
        }

        System.out.println("是否修改商家介绍(y/n)");
        inputStr = input.next();

        //忽略大小写
        if(inputStr.equals("y") || inputStr.equals("Y")){
            System.out.println("输入新的商家介绍");
            business.setBusinessExplain(input.next());
        }

        System.out.println("是否修改起送费用(y/n)");
        inputStr = input.next();

        //忽略大小写
        if(inputStr.equals("y") || inputStr.equals("Y")){
            System.out.println("输入新的起送费用");
            business.setStarPrice(input.nextDouble());
        }

        System.out.println("是否修改配送费用(y/n)");
        inputStr = input.next();

        //忽略大小写
        if(inputStr.equals("y") || inputStr.equals("Y")){
            System.out.println("输入新的配送费用");
            business.setDeliveryPrice(input.nextDouble());
        }

        //DAO层更新操作
        int result = dao.updateBusiness(business);

        if(result>0){
            System.out.println("\n修改商家信息成功\n");
        }else{
            System.out.println("\n修改商家信息失败\n");
        }
    }

    /**
     * 更新商家密码
     * @param business
     */
    @Override
    public void updateBusinessByPassword(Integer businessId) {
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);

        System.out.println("\n请输入旧密码: ");
        String oldPass = input.next();
        System.out.println("\n请输入新密码: ");
        String newPass = input.next();
        System.out.println("\n再次输入新密码: ");
        String beginNewPass = input.next();

        if(!business.getPassword().equals(oldPass)) {
            System.out.println("\n旧密码输入错误");
        }else if(!newPass.equals(beginNewPass)){
            System.out.println("两次输入的密码不一致");
        }else{
            int result = dao.updateBusinessByPassword(businessId,newPass);
            if(result>0){
                System.out.println("\n修改密码成功");
            }else{
                System.out.println("\n修改密码失败");
            }
        }

    }
}
